﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizOptionLabel : CustBizObject
    {
        private CDmDjkOptionLabel m_CDmDjkOptionLabel = new CDmDjkOptionLabel();
        private CDmDjkOptionLabel oDmDjkOptionLabel
        {
            get { return m_CDmDjkOptionLabel; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizOptionLabel()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkOptionLabel.DJK_OPTION_LABEL);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 根据optionId查询label信息
        /// </summary>
        /// <param name="optionId">optionId值</param>
        /// <returns></returns>
        /// Author:刘魏
        /// Creation date:2015-8-31 11:10
        public DataSet GetOptionLabelByOption(string optionId)
        {
            if (string.IsNullOrWhiteSpace(optionId))
            {
                return new DataSet();
            }

            DataSet ds = new DataSet();

            string sqlStr = @"SELECT * 
                                FROM " + this.TableName + @" 
                                WHERE OPTION_ID = @optionid 
                                    AND LABEL_IS_VALID = 1 
                                    AND LABEL_IS_SHOW = 1 
                                    AND LABEL_IS_DELETED = 0";
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("optionid", ParameterDirection.Input, EDbType.String, 36, optionId));

            ds = this.DataObj.QueryBySQL(sqlStr, parmCollection);

            return ds;
        }

        /// <summary>
        /// 添加optionLabel信息
        /// </summary>
        /// <param name="optionId">optionId</param>
        /// <param name="labelName">名称</param>
        /// <param name="labelFunCode">类型代码</param>
        /// <param name="startVal">开始值</param>
        /// <param name="endVal">结束值</param>
        /// <param name="isOnval">是否为单值(1、单值，0、非单值)</param>
        /// <param name="isValid">是否有效(1、有效，0、无效)</param>
        /// <param name="isShow">是否显示(1、显示，0、不显示)</param>
        /// <returns>是否添加成功</returns>
        /// Author:刘魏
        /// Creation date:2015-8-31 11:10
        public bool AddOptionLabelInfo(string optionId, string labelName, string labelFunCode, string startVal, string endVal, int isOnval, int isValid, int isShow)
        {
            DataSet ds = new CDmDjkOptionLabel();
            DataRow dr = ds.Tables[0].NewRow();

            dr["ID"] = this.DataObj.NewID();
            dr["OPTION_ID"] = optionId;
            dr["OPTION_LABEL_CODE"] = msBsc.SvRef.ApplicationConst.Const.GetNewSerialNo("OPTION_LABEL_CODE", "", 4);
            dr["OPTION_LABEL_NAME"] = labelName;
            dr["LABEL_FUN_CODE"] = labelFunCode;
            dr["LABEL_START_VAL"] = startVal;
            dr["LABEL_END_VAL"] = endVal;
            dr["LABEL_IS_ONVAL"] = isOnval;
            dr["INIT_USR"] = msBsc.SvRef.UserInfo.Operator;
            dr["INIT_DT"] = this.dtCurrDateTime;
            dr["LABEL_IS_VALID"] = isValid;
            dr["LABEL_IS_SHOW"] = isShow;
            dr["LABEL_IS_DELETED"] = 0;

            ds.Tables[0].Rows.Add(ds);

            this.Update(ds);
            return true;
        }
    }
}
